package com.example.s.view;

import android.content.res.ColorStateList;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.StaggeredGridLayoutManager;
import androidx.viewpager.widget.ViewPager;

import com.example.s.R;
import com.example.s.adapter.InnerHolder;
import com.example.s.adapter.MyCommonViewAdapter;
import com.example.s.adapter.MyFragmentPagerAdapter;
import com.example.s.adapter.MyRecyclerViewAdapter;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

public class MyNav<T, A> {

    String TAG = "MyNav";
    private final TabLayout tabLayout;
    private ViewPager viewPager;
    private final List<NavItem> data;
    private final OnMyNavListener listener;
    private List<Fragment> vpData;
    private final List<T> vpCommentData = new ArrayList<>();
    private FragmentManager fm;
    private MyCommonViewAdapter<T> adapter;
    private OnRecyclerCallBack<A> commonVpCallBack;
    private MyRecyclerViewAdapter<A> recyclerViewAdapter;
    private int recyclerViewItemId;
    private RecyclerView.LayoutManager manager;
    private RecyclerView.RecycledViewPool recycledViewPool = new RecyclerView.RecycledViewPool();

    /**
     * 普通导航栏
     *
     * @param tabLayout
     * @param data
     * @param listener
     */
    public MyNav(TabLayout tabLayout, List<NavItem> data, OnMyNavListener listener) {
        this.tabLayout = tabLayout;
        this.data = data;
        this.listener = listener;
        initNav();
    }


    /**
     * 导航栏 + viewpager recyclerview适配
     */
    public MyNav(TabLayout tabLayout, ViewPager viewPager, List<NavItem> data, OnMyNavListener listener) {
        this.tabLayout = tabLayout;
        this.viewPager = viewPager;
        this.data = data;
        this.listener = listener;
        initRecyclerVP(); //必须在前
    }

    /**
     * 导航栏 + viewpager  fragment 适配
     */
    public MyNav(TabLayout tabLayout, ViewPager viewPager, List<NavItem> data, FragmentManager fm, List<Fragment> vpData, OnMyNavListener listener) {
        this.tabLayout = tabLayout;
        this.viewPager = viewPager;
        this.data = data;
        this.fm = fm;
        this.vpData = vpData;
        this.listener = listener;
        initFragmentVP(); //必须在前
        initNav();
    }


    /**
     * 设置recyclerview数据源
     *
     * @param vpCommentData
     * @param recyclerViewItemId
     * @return
     */
    public MyNav<T, A> setVpRecyclerData(RecyclerView.LayoutManager manager, List<T> vpCommentData, int recyclerViewItemId) {
        this.manager = manager;
        this.vpCommentData.clear();
        this.vpCommentData.addAll(vpCommentData);
        this.recyclerViewItemId = recyclerViewItemId;
        adapter.notifyDataSetChanged();
        initNav();
        viewPager.setCurrentItem(0);
        return this;
    }

    private RecyclerView.LayoutManager[] getLayoutManangerArr(RecyclerView.LayoutManager manager) {
        RecyclerView.LayoutManager[] managers = new RecyclerView.LayoutManager[data.size()];
        for (int i = 0; i < data.size(); i++) {
            if (manager.getClass() == LinearLayoutManager.class) {
                managers[i] = new LinearLayoutManager(viewPager.getContext(), ((LinearLayoutManager) manager).getOrientation(), false);
            } else if (manager.getClass() == GridLayoutManager.class) {
                managers[i] = new GridLayoutManager(viewPager.getContext(), ((GridLayoutManager) manager).getSpanCount(), ((GridLayoutManager) manager).getOrientation(), false);

            } else {
                managers[i] = new StaggeredGridLayoutManager(((StaggeredGridLayoutManager) manager).getSpanCount(), ((StaggeredGridLayoutManager) manager).getOrientation());

            }

        }

        return managers;
    }


    /**
     * 处理recyclerview viewholder 回调
     *
     * @param commonVpCallBack
     * @return
     */
    public MyNav<T, A> setRecyclerCallBack(OnRecyclerCallBack<A> commonVpCallBack) {
        this.commonVpCallBack = commonVpCallBack;
        return this;
    }


    /**
     * 初始化viewpager
     */
    private void initRecyclerVP() {
        //适配每一页的高度
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            }

            @Override
            public void onPageSelected(int position) {
                viewPager.requestLayout();
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

        viewPager.setAdapter(adapter = new MyCommonViewAdapter<T>(vpCommentData) {
            @Override
            public Object handleData(ViewGroup container, int pos) {
                RecyclerView recyclerView = new RecyclerView(container.getContext());
                recyclerView.setRecycledViewPool(recycledViewPool);
                LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                recyclerView.setMinimumHeight(1920); //屏幕高度?
                Log.d(TAG, "handleData: pos" + pos);
                recyclerView.setLayoutManager(getLayoutManangerArr(manager)[pos]);
                recyclerView.setNestedScrollingEnabled(false);
                recyclerView.setAdapter(recyclerViewAdapter = new MyRecyclerViewAdapter<A>((List<A>) vpCommentData.get(pos), recyclerViewItemId) {
                    @Override
                    public void handleData(InnerHolder holder, Object item) {
                        if (commonVpCallBack != null) {
                            commonVpCallBack.handleRecyclerData(recyclerView, recyclerViewAdapter, holder, (A) item);
                        }
                    }
                });
                container.addView(recyclerView, layoutParams);
                return recyclerView;
            }
        });
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setOffscreenPageLimit(data.size());
    }


    private void initFragmentVP() {
        viewPager.setAdapter(new MyFragmentPagerAdapter(fm, vpData));
        tabLayout.setupWithViewPager(viewPager);
        viewPager.setOffscreenPageLimit(data.size());
        viewPager.setCurrentItem(0);
    }

    /**
     * 初始化 nav
     */
    private void initNav() {
        for (int i = 0; i < data.size(); i++) {
            NavItem navItem = data.get(i);
            tabLayout.getTabAt(i).setCustomView(getview(navItem.icon, navItem.title));

        }
        ImageView imageView = tabLayout.getTabAt(0).getCustomView().findViewById(R.id.iv);
        TextView tv = tabLayout.getTabAt(0).getCustomView().findViewById(R.id.title);
        listener.onSelectListener(imageView, tv, 0);
        tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                ImageView imageView = tab.getCustomView().findViewById(R.id.iv);
                TextView tv = tab.getCustomView().findViewById(R.id.title);
                listener.onSelectListener(imageView, tv, tabLayout.getSelectedTabPosition());
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                ImageView imageView = tab.getCustomView().findViewById(R.id.iv);
                TextView tv = tab.getCustomView().findViewById(R.id.title);
                listener.onCommonListener(imageView, tv);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {

            }
        });


    }

    /**
     * 获取 navitem  view
     *
     * @param icon
     * @param titleText
     * @return
     */

    private View getview(int icon, String titleText) {
        View view = LayoutInflater.from(tabLayout.getContext()).inflate(R.layout.main_nav_bottom, null);
        if (icon == R.mipmap.home_page_upload) {
            return view;
        }
        ImageView iv = view.findViewById(R.id.iv);
        if (icon != 0)
            iv.setBackgroundResource(icon);
        TextView title = view.findViewById(R.id.title);
        if (!"".contentEquals(titleText))
            title.setText(titleText);
        return view;
    }

    public interface OnRecyclerCallBack<A> {
        void handleRecyclerData(RecyclerView view, MyRecyclerViewAdapter<A> adapter, InnerHolder holder, A item);

    }

    public interface OnMyNavListener {
        void onCommonListener(ImageView icon, TextView title);

        void onSelectListener(ImageView icon, TextView title, int pos);
    }

    public static class NavItem {
        private int icon;
        private String title;

        public NavItem(int icon, String title) {
            this.icon = icon;
            this.title = title;
        }

        public NavItem(String title) {
            this.title = title;
        }

        public int getIcon() {
            return icon;
        }

        public void setIcon(int icon) {
            this.icon = icon;
        }

        public String getTitle() {
            return title;
        }

        public void setTitle(String title) {
            this.title = title;
        }
    }
}
